#!/usr/bin/env python
# coding=utf-8
# Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved.
# MemCache_Hybrid is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#          http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.

"""Brief description of the module.
   Interactive calling of memcache_hybrid
   Available commands: put, get, remove, quit
"""

import faulthandler

from memcache_hybrid import DistributedObjectStore
from memcache_hybrid import ReplicateConfig
import acl

faulthandler.enable()  # 崩溃时自动打印 Python 堆栈
acl.init()
count, ret = acl.rt.get_device_count()
print("设备数量:", acl.rt.get_device_count())
ret = acl.rt.set_device(count - 1)
print("set_device returned: {}".format(ret))



STORE = DistributedObjectStore()
DEFAULT_REPLICA_NUMBER = 2
AVAILABLE_COMMANDS = "'put <key> <value>', 'get <key>', 'remove <key>' or 'quit'/'exit'"


def set_up():
    res = STORE.init(count - 1)
    if res != 0:
        raise ValueError(f"init failed, res={res}")
    print("object store set_up succeeded.")


def put(key: str, value: bytes):
    print(f"Executing put operation: key={key}, value={value}")
    config = ReplicateConfig()
    config.replicaNum = DEFAULT_REPLICA_NUMBER
    res = STORE.put(key, value, config)
    if res != 0:
        raise ValueError(f"put failed, res={res}")
    print(f"Put operation succeeded")


def get(key: str):
    print(f"Executing get operation: key={key}")
    retrieved_data = STORE.get(key)
    print(f"Get result: {retrieved_data}")


def remove(key: str):
    print(f"Executing remove operation: key={key}")
    res = STORE.remove(key)
    if res != 0:
        raise ValueError(f"remove failed, res={res}")
    print(f"Remove operation succeeded")


def stop():
    print("Performing cleanup (stop)...")
    STORE.close()
    print("object store destroyed")


def main():
    print("Welcome to the interactive command tool")
    print(f"Available commands: {AVAILABLE_COMMANDS}")
    set_up()
    while True:
        try:
            user_input = input("Enter command: ").strip()
            if not user_input:
                print(f"Invalid command. Please enter {AVAILABLE_COMMANDS}")
                continue

            parts = user_input.split()
            command = parts[0].lower()

            if command == 'put':
                if len(parts) != 3:
                    print("Usage: put <key> <value>")
                else:
                    put(parts[1], bytes(parts[2], "utf-8"))
            elif command == 'get':
                if len(parts) != 2:
                    print("Usage: get <key>")
                else:
                    get(parts[1])
            elif command == 'remove':
                if len(parts) != 2:
                    print("Usage: remove <key>")
                else:
                    remove(parts[1])
            elif command in ('quit', 'exit'):
                print("Preparing to exit...")
                break
            else:
                print(f"Invalid command. Please enter {AVAILABLE_COMMANDS}")

        except KeyboardInterrupt:
            print("\nInterrupt received (Ctrl+C). Preparing to exit...")
            break
        except EOFError:
            print("\nEnd of input detected. Preparing to exit...")
            break


if __name__ == '__main__':
    try:
        main()
    finally:
        stop()  # Ensure cleanup runs regardless of how the program exits